var t = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    card_id:"",
    iconsUrl:t.globalData.iconsUrl,
    countList:[], 
    pageSize:10,
    list:[],
    page:1,// 当前页码
    last_page:1,
    totalCount:0,//总条数
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    isScrollAble:true,
    refreshOpen:true, //下拉刷新/下拉复位
    refreshstatus:true, //是否开启下拉刷新
    topNum:0, 
    search:"",
    //  
    userinfo:{}, 
    dataType:0,
    showStatus:{},
    // 延期弹窗
    showPopup:false,
    user_coupon_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    that.setListHeight()
    that.setData({
      page:1,
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });

    if(options.card_id != undefined){
      that.setData({
        card_id:options.card_id
      })
      that.getDataList()
    }
    
  },
  // 获取滚动条当前位置
  scrolltoupper:function(e){
  },
  //一键回到顶部
  backTop: function (e) {
    this.setData({topNum:0});
  },
  // 选项卡
  swichNav:function(e){
    this.setData({
      dataType:e.currentTarget.dataset.current,
      topNum:0,
      page:1,
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  showDetail(e){
    let showStatus = this.data.showStatus
    showStatus[e.currentTarget.dataset.id] = !showStatus[e.currentTarget.dataset.id]
    this.setData({
      showStatus
    })
    console.log('showStatus',showStatus)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  onShowPopup(){
    this.setData({
      showPopup: !this.data.showPopup
    }); 
  },
  changCard(e){
    let that =this
    let type = e.currentTarget.dataset.type
    // 延期
    if(type==1){
      that.setData({
        showPopup: !that.data.showPopup,
        user_coupon_id :e.currentTarget.dataset.id
      }); 
    }
    // 作废
    if(type==2){
      let obj={
        user_coupon_id :e.currentTarget.dataset.id
      }
      wx.showModal({
        title: '信息提示',
        content: '确定要作废优惠券？',
        showCancel: true,
        success(res) {
          if (res.confirm) {
            t.request('/Manage/Management/cancelUserCoupon',obj,function(res){
              if (res.code == "200") {
                wx.showToast({ title:res.msg,icon: 'success',duration: 2000})   
                that.getDataList(); 
              } 
            })
          }else{
            that.getDataList();
          }
          
        }
      });
    }
  },
  //提交
  formSubmit(e){
    let that =  this
    let data =e.detail.value
    let obj = {
      user_coupon_id:that.data.user_coupon_id,
      days:data.p_number,
    }
    t.request('/Manage/Management/extendUserCoupon',obj,function(e){
        if (e.code == "200") {
          t.showSuccessToast(e.msg, function() {
            that.getDataList()          //刷新
            that.setData({
              showPopup: !that.data.showPopup
            }); 
          });
        } else{
          // 设置为可以提交的状态
          t.showError(e.msg);
        }
    })
},
  //获取数据列表
  getDataList(isPage, page){
    var that = this
    var o = {
      card_id:that.data.card_id,
      page: page || 1,
      size:that.data.pageSize,
      type:that.data.dataType
    }
    t.request('/Manage/Management/getUserCoupon',o,function(e){
        // 刷新状态复位
        that.setData({refreshOpen: 0 });
        // 不加这个方法真机下拉会一直处于刷新状态，无法复位
        if (e.code == "200") {
          let resList = e.result,dataList = that.data.list;
          let list = resList.list;
          let showStatus=that.data.showStatus
          list.forEach(element => {
            showStatus[element.user_coupon_id] = false
          });
          that.setData({
            showStatus
          })
          if (isPage == true) {
            that.setData({
              list:dataList.concat(list),
              isLoading: false,
              last_page: resList.total,
              totalCount: resList.total,
            })
          }else{
            that.setData({
              list,
              isLoading: false,
              last_page: resList.total,
              totalCount: resList.total,
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000
          })
        }
    })
  },

  // 自定义下拉刷新
  upRefresh(){
    this.setData({
      list:[],
      page:1,// 当前页码
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    })
    this.getDataList()
  },
   /**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    // 已经是最后一页
    console.log("翻页");
    console.log("page =",this.data.page)
    console.log("last_page =",this.data.last_page)
    if (this.data.page > this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },
  /**
   * 设置文章列表高度
   */
  setListHeight() {
    const systemInfo = wx.getWindowInfo();
    console.log('systemInfo',systemInfo)
    let tabBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
    // pad + windows
    if(systemInfo.pixelRatio <=2){
        // windows
        if(systemInfo.statusBarHeight ==0){
          tabBarHeight = (tabBarHeight-60)*systemInfo.pixelRatio
        }else{
          // pad
          // tabBarHeight = tabBarHeight*systemInfo.pixelRatio
          tabBarHeight = tabBarHeight-25
        }
    }else{
      tabBarHeight =(tabBarHeight+25)*systemInfo.pixelRatio
    }
    console.log('tabbar高度：',tabBarHeight+'rpx')
    this.setData({
        scrollHeight: tabBarHeight,
      });
  },

  // 页面跳转
  goUrl(e){
    let url = e.currentTarget.dataset.url
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})